﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using RenRen.Rest;
using RenRen.Session;
using System.Web.Configuration;
using System.Web;

namespace RenRen.Web.Mvc {
    public static class ControllerExtension {
        public static bool IsXnml { get; set; }

        /// <summary> 
        /// The APi key for this application given by renren
        /// </summary>
        public static string ApiKey { get { return WebConfigurationManager.AppSettings["ApiKey"]; } }

        /// <summary> 
        /// The APi Secret for this application given by renren
        /// </summary>
        public static string Secret { get { return WebConfigurationManager.AppSettings["Secret"]; } }

        public static bool ConnectRenRen(this Controller controller) {
            var api = controller.GetApi();

            SetCookie("User", api.Users.GetLoggedInUser().ToString());
            return true;
        }

        public static Api GetApi(this Controller controller, string apiKey, string secret) {
            XnmlCanvasSession session = new XnmlCanvasSession(apiKey ?? WebConfigurationManager.AppSettings["ApiKey"], secret ?? WebConfigurationManager.AppSettings["Secret"]);
            return new Api(session);
        }
        public static Api GetApi(this Controller controller) {
            CanvasSession session = null;
            if (IsXnml) {
                session = new XnmlCanvasSession(ApiKey ?? WebConfigurationManager.AppSettings["ApiKey"], Secret ?? WebConfigurationManager.AppSettings["Secret"]);
            } else {
                session = new IFrameCanvasSession(ApiKey ?? WebConfigurationManager.AppSettings["ApiKey"], Secret ?? WebConfigurationManager.AppSettings["Secret"]);
            }
            return new Api(session);
        }
        public static int GetCurrentUid(this Controller controller) {
            if (GetCookie("User") == null)
                controller.ConnectRenRen();
            return int.Parse(GetCookie("User"));
        }

        private static string GetCookie(string cookieName) {
            string retString = null;
            string fullCookie = ApiKey + "_" + cookieName;

            if (HttpContext.Current != null) {
                if (HttpContext.Current.Request.Cookies[fullCookie] != null)
                    retString = HttpContext.Current.Request.Cookies[fullCookie].Value;
            }

            return retString;
        }

        private static void SetCookie(string cookieName, string val) {
            string fullCookie = ApiKey + "_" + cookieName;

            if (HttpContext.Current != null) {
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(fullCookie, val));
            }
        }

        public static void ClearCookies() {
            string[] cookies = new[] { "user", "session_key", "expires", "ss" };
            foreach (var c in cookies) {
                string fullCookie = ApiKey + "_" + c;

                if (HttpContext.Current != null &&
                    HttpContext.Current.Response.Cookies[fullCookie] != null) {
                    HttpContext.Current.Response.Cookies[fullCookie].Expires = DateTime.Now.AddMonths(-1);
                }
            }
        }
    }
}